﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;
using System.Web;
using System.IO;

namespace Core
{
    public static partial class system
    {
        public static dynamic uploadFile(HttpPostedFileBase userfile, string ext)
        {
            var path = ConfigurationManager.AppSettings["UploadPath"];

            MD5CryptoServiceProvider get_md5 = new MD5CryptoServiceProvider();
            byte[] hash_byte = get_md5.ComputeHash(userfile.InputStream);
            string hash = BitConverter.ToString(hash_byte).Replace("-", "");

            var oldfile = Directory.GetFiles(path, string.Format("*_{0}{1}", hash, ext), SearchOption.AllDirectories);
            if (oldfile.Length != 0) path = Path.GetFileName(oldfile[0]);
            else
            {
                var time = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
                path = string.Format("{0}{1}_{2}{3}", path, time, hash, ext);
                userfile.SaveAs(path);
                path = Path.GetFileName(path);
            }
            return new
            {
                success = true,
                filename = path
            };
        }
    }
}
